package vip.meeet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import vip.meeet.utils.HttpUtil;

import java.io.IOException;

/**
 * 动态模板：https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html
 * https://www.elastic.co/guide/en/elasticsearch/reference/7.5/mapping.html
 */
public final class MappingHelper {

    private static final String MAPPING_URL_BASE =  "http://%s/%s/_mapping?pretty";

    private static final String IP_PORT_BASE =  "%s:%d";

    private static final String DEFAULT_HOST = "127.0.0.1";

    private static final int DEFAULT_PORT = 9200;

    public static String getMapping(String index) throws IOException {
        return getMapping(DEFAULT_HOST,DEFAULT_PORT,index);
    }

    public static String getMapping(String ip,Integer port,String index) throws IOException {
        String host = String.format(IP_PORT_BASE, ip, port);
        return getMapping(host,index);
    }

    public static String getMapping(String hostPort,String index) throws IOException {
        String url = String.format(MAPPING_URL_BASE, hostPort,index);
        String content = HttpUtil.getContent(url);
        return content;
    }

    /**
     * 获取指定索引的mappings部分
     * @param host
     * @param index
     * @param type
     * @return
     */
    public static String getAddMapping(String host,String index,String type) throws IOException {
        String mappingJson = getMapping(host, index);
        JSONObject indexObject = JSON.parseObject(mappingJson);
        JSONObject mappingObject = indexObject.getJSONObject(index);
        JSONObject result = new JSONObject();
        JSONObject properties;
        JSONObject mappingsJson;
        if(mappingObject != null){
            mappingsJson = mappingObject.getJSONObject("mappings");
            if(mappingsJson != null){
                if(type == null) {
                    type = "_doc";
                }
                properties = mappingsJson.getJSONObject(type);
                if(properties != null){
                    result.put("mappings",properties);
                }else {
                    result = mappingsJson;
                }
            }
        }else {
            result.put("mappings",new JSONObject());
        }
        return result.toJSONString();
    }

    /**
     * 获取指定索引的mappings部分
     * @param host
     * @param index
     * @return
     */
    public static String getAddMapping(String host,String index) throws IOException {
        //{"index":{"mappings":{"type":{"properties":{}}}}} type部分可能不存在
        String mappingJson = getMapping(host, index);
        if(StringUtils.isBlank(mappingJson)){
            return getDefaultMapping();
        }
        JSONObject indexObject = JSON.parseObject(mappingJson);
        JSONObject mappingObject = indexObject.getJSONObject(index);
        JSONObject mappings = mappingObject.getJSONObject("mappings");
        String mapJson = mappings.toJSONString();
        int start = mapJson.indexOf("properties");
        if(start == -1){
            return getDefaultMapping();
        }
        String result = mapJson.substring(start - 2, mapJson.length() - 1);
        return String.format("{\"%s\":%s}","mappings",result);
    }

    /**
     *  获取指定索引的类型，主要是7.x之前的版本
     * @param host
     * @param index
     * @return
     */
    public static String getIndexType(String host,String index) throws IOException {
        //{"index":{"mappings":{"type":{"properties":{}}}}} type部分可能不存在
        String mappingJson = getMapping(host, index);
        if(StringUtils.isBlank(mappingJson)){
            return getDefaultMapping();
        }
        JSONObject indexObject = JSON.parseObject(mappingJson);
        JSONObject mappingObject = indexObject.getJSONObject(index);

        JSONObject mappings = mappingObject.getJSONObject("mappings");
        if(mappings.getJSONObject("properties") != null){//如果mappings下直接就是properties，说明没有type
            return null;
        }
        String mapJson = mappings.toJSONString();
        int start = mapJson.indexOf("\"");
        if(start == -1){
            return null;
        }
        int end = mapJson.indexOf("\"", start + 1);
        return mapJson.substring(start + 1,end);
    }

    private static String getDefaultMapping(){
        JSONObject properties = new JSONObject();
        properties.put("properties",new JSONObject());
        JSONObject mappings = new JSONObject();
        mappings.put("mappings",properties);
        return mappings.toJSONString();
    }

    public static String getUpdateMapping(String host,String index,String type) throws IOException {
        String mappingJson = getMapping(host, index);
        JSONObject indexObject = JSON.parseObject(mappingJson);
        JSONObject mappingObject = indexObject.getJSONObject(index);
        JSONObject result = new JSONObject();
        JSONObject properties;
        JSONObject mappingsJson;
        if(mappingObject != null){
            mappingsJson = mappingObject.getJSONObject("mappings");
            if(mappingsJson != null){
                if(type == null) {
                    type = "_doc";
                }
                properties = mappingsJson.getJSONObject(type);
                if(properties != null){
                    return properties.toJSONString();
                }
            }
        }else {
            result.put("properties",new JSONObject());
        }
        return result.toJSONString();
    }

}
